package ui;

import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class Login extends JFrame {
    private Socket socket;

    public Login() {
        setTitle("局域网聊天室");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(new Color(245, 245, 245));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);

        // 昵称标签
        JLabel nicknameLabel = new JLabel("名称:");
        nicknameLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        nicknameLabel.setForeground(new Color(60, 60, 60));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.LINE_END;
        panel.add(nicknameLabel, gbc);

        // 昵称输入框
        JTextField nicknameText = new JTextField(15);
        nicknameText.setPreferredSize(new Dimension(200, 35));
        nicknameText.setFont(new Font("微软雅黑", Font.PLAIN, 16));
//        nicknameText.setVisible(true);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.LINE_START;
        panel.add(nicknameText, gbc);



        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
        buttonPanel.setBackground(new Color(245, 245, 245));

        JButton enterButton = new JButton("进入");
        enterButton.setBackground(new Color(220, 53, 69));
        enterButton.setForeground(Color.WHITE);
        enterButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        enterButton.setFocusPainted(false);
        enterButton.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
        enterButton.setContentAreaFilled(true);
        enterButton.setOpaque(true);
        enterButton.setBorderPainted(false);
        enterButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        JButton cancelButton = new JButton("取消");
        cancelButton.setBackground(new Color(0, 123, 255));
        cancelButton.setForeground(Color.WHITE);
        cancelButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        cancelButton.setFocusPainted(false);
        cancelButton.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
        cancelButton.setContentAreaFilled(true);
        cancelButton.setOpaque(true);
        cancelButton.setBorderPainted(false);
        cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        buttonPanel.add(enterButton);
        buttonPanel.add(cancelButton);

        //添加监听器
        enterButton.addActionListener(e -> {
            String nickname = nicknameText.getText();
            nicknameText.setText("");
            if (!nickname.isEmpty()) {
                try {
                    loging(nickname);
                    new ChatFrame(nickname, socket);
                    dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }


            } else {
                ChatFrame chatFrame = new ChatFrame();
                chatFrame.setVisible(true);
                this.setVisible(false);
            }
        });

        cancelButton.addActionListener(e -> {
            System.exit(0);
        });

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(buttonPanel, gbc);

        add(panel);
        this.setVisible(true);
    }

    private void loging(String nickname) throws Exception {

        socket = new Socket(Constant.SERVER_IP, Constant.SERVER_PORT);
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeInt(1);
        dos.writeUTF(nickname);
        dos.flush();


    }


}